package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.判断添加的是菜品还是套餐
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        ShoppingCart Usercart = new ShoppingCart();
        if (dishId != null) {
            //添加的是菜品
            //判断当前菜品是否在购物车中
            DishDTO dish = dishMapper.getById(dishId);
            shoppingCart.setDishId(dishId);
            if (shoppingCartDTO.getDishFlavor()!= null){
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            }
            ShoppingCart UserCart = shoppingCartMapper.getInfo(shoppingCart);
            if (UserCart != null){
                // 如果有口味，则判断口味是否相同
                if(shoppingCartDTO.getDishFlavor() !=null){
                    if (shoppingCartDTO.getDishFlavor().equals(UserCart.getDishFlavor())){
                        UserCart.setNumber(UserCart.getNumber() + 1);
                        shoppingCartMapper.update(UserCart);
                        return;
                    }else {
                        Usercart.setDishId(dishId);
                        Usercart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                        Usercart.setImage(dish.getImage());
                        Usercart.setName(dish.getName());
                        Usercart.setAmount(dish.getPrice());
                    }
                }else {
                    UserCart.setNumber(UserCart.getNumber() + 1);
                    shoppingCartMapper.update(UserCart);
                    return;
                }

            } else {
                Usercart.setDishId(dishId);
                Usercart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                Usercart.setImage(dish.getImage());
                Usercart.setName(dish.getName());
                Usercart.setAmount(dish.getPrice());
            }
        }else {
            Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            ShoppingCart UserCart = shoppingCartMapper.getInfo(shoppingCart);
            if (UserCart != null){
                UserCart.setNumber(UserCart.getNumber() + 1);
                shoppingCartMapper.update(UserCart);
                return;
            }else {
                Usercart.setSetmealId(shoppingCartDTO.getSetmealId());
                Usercart.setImage(setmeal.getImage());
                Usercart.setName(setmeal.getName());
                Usercart.setAmount(setmeal.getPrice());
            }
        }
        Usercart.setUserId(userId);
        Usercart.setNumber(1);
        Usercart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(Usercart);
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 删除购物车其中一条数据
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        ShoppingCart cart = shoppingCartMapper.getInfo(shoppingCart);
        if (cart.getNumber() == 1){
            shoppingCartMapper.deleteOne(shoppingCart);
        }else {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }
}
